package com.maven.front.service.eduArticle;

import com.maven.front.bean.EduArticle;

import com.maven.front.bean.EduComment;
import com.maven.front.bean.EduUser;
import com.maven.front.dto.EduArticleDto;
import com.maven.front.dto.EduCommentDto;
import com.maven.front.mapper.EduArticleContentMapper;
import com.maven.front.mapper.EduArticleMapper;

import com.maven.front.mapper.EduCommentMapper;
import com.maven.front.mapper.EduUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class EduArticleService {

    @Resource
    private EduArticleMapper eduArticleMapper;
    @Resource
    private EduArticleContentMapper eduArticleContentMapper;
    @Resource
    private EduCommentMapper eduCommentMapper;
    @Resource
    private EduUserMapper eduUserMapper;


    //查询
    public List<EduArticleDto> listAll(EduArticle eduArticle) {
        List<EduArticleDto> listEduArticleDto=new ArrayList<>();
        List<EduArticle> listEduArticle=eduArticleMapper.list(eduArticle);
        System.out.println(listEduArticle.size()+"长度");
        for (int i=0;i< listEduArticle.size();i++){
            //获取副表中的content
//            System.out.println(eduArticleContentMapper.selectid(listEduArticle.get(i).getArticleId()));
            String content=eduArticleContentMapper.selectid(listEduArticle.get(i).getArticleId()).getContent();
            EduArticleDto eduArticleDto=new EduArticleDto();
            //  将父类数据复制给子类数据
            BeanUtils.copyProperties(listEduArticle.get(i),eduArticleDto);
            //将获取的名称赋值给子类的新属性(name)
            eduArticleDto.setContent(content);
            listEduArticleDto.add(eduArticleDto);
        }
        if(listEduArticleDto!=null){
            return listEduArticleDto;
        }else{
            return null;
        }
    }


    //根据id查询
    public  EduArticle selectByPrimaryKey(Integer id){

        return eduArticleMapper.selectByPrimaryKey(id);
    }

    //查询评论
    public List<EduCommentDto> selectAll(Integer id){
        List<EduCommentDto> listeduCommentDto=new ArrayList<>();
        List<EduComment> listComment=eduCommentMapper.selectAll(id);
    for (int i=0;i<listComment.size();i++){
        String userName=eduUserMapper.getid(listComment.get(i).getUserId()).getUserName();
        System.out.println(userName);
        EduCommentDto eduCommentDto=new EduCommentDto();
        BeanUtils.copyProperties(listComment.get(i),eduCommentDto);
        eduCommentDto.setUserName(userName);
        listeduCommentDto.add(eduCommentDto);

    }
        if(listeduCommentDto!=null){
            return listeduCommentDto;
        }else{
            return null;
        }
    }




}
